import { Config, Provide } from '@midwayjs/core';
import { Configuration, OpenAIApi } from 'openai';
import { AI } from '../ai/ai.interface';
// import {AIModelGPT35} from '../ai/AIModel.enum';

@Provide()
export default class ChatGPTService implements AI {
  @Config('openai')
  config;

  openai: OpenAIApi;

  getOpenai() {
    if (!this.openai) {
      const config = new Configuration(this.config);
      this.openai = new OpenAIApi(config);
    }
    return this.openai;
  }

  async chat(prompt: string) {
    const openai: OpenAIApi = this.getOpenai();
    const completion = await openai.createChatCompletion({
      max_tokens: 1000,
      temperature: 0.1,
      user: 'user',
      model: AIModelGPT35.CHAT_GPT3_5_TURBO,
      n: 1,
      messages: [
        {
          role: 'system',
          content: '你好，我是赵琳同学的小助手。',
        },
        {
          role: 'user',
          content: prompt,
        },
        {
          role: 'assistant',
          content: '你好，我是赵琳同学的小助手。',
        },
      ],
    });
    return completion.data;
  }

  async translate() {}

  async summary() {}

  tranning() {
    throw new Error('Method not implemented.');
  }
}
